package org.xqyy.eval.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xqyy.eval.service.IExternalEvalMapperService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by luoxiaoming on 16-12-25.
 */

@Controller
@RequestMapping("/externalEval")
public class ExternalEvalMapperController {

    @Autowired
    private IExternalEvalMapperService externalEvalMapperService;

    @RequestMapping("/loadEvalMappers")
    @ResponseBody
    public Map<String, Object> loadEvalMappers(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        String groupCode = request.getParameter("groupCode");
        if (StringUtils.hasLength(groupCode)) {
            res.put("groupCode", groupCode);
        }
        String deptCode = request.getParameter("deptCode");
        if (StringUtils.hasLength(deptCode)) {
            res.put("deptCode", deptCode);
        }
        String paperId = request.getParameter("paperId");
        if (StringUtils.hasLength(paperId)) {
            res.put("paperId", paperId);
        }
        List<Map<String, Object>> data = externalEvalMapperService.load(res);
        res.put("root", data);
        return res;
    }

    @RequestMapping("/addEval")
    @ResponseBody
    public Map<String, Object> addEval(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        try {
            String groupCode = request.getParameter("groupCode");
            String deptCode = request.getParameter("deptCode");
            String paperId = request.getParameter("paperId");
            String mark = request.getParameter("mark");
            String weights = request.getParameter("weights");
            res.put("groupCode", groupCode);
            res.put("deptCode", deptCode);
            res.put("paperId", paperId);
            res.put("mark", mark);
            res.put("weights", weights);
            externalEvalMapperService.add(res);
            res.put("success", "操作成功!");
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/updateEval")
    @ResponseBody
    public Map<String, Object> updateEval(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        try {
            String id = request.getParameter("id");
            String groupCode = request.getParameter("groupCode");
            String targetGroupCode = request.getParameter("targetGroupCode");
            String paperId = request.getParameter("paperId");
            String mark = request.getParameter("mark");
            String weights = request.getParameter("weights");
            res.put("id", id);
            res.put("groupCode", groupCode);
            res.put("targetGroupCode", targetGroupCode);
            res.put("paperId", paperId);
            res.put("mark", mark);
            res.put("weights", weights);
            externalEvalMapperService.modify(res);
            res.put("success", "操作成功!");
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/deleteEval")
    @ResponseBody
    public Map<String, Object> deleteEval(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        try {
            String id = request.getParameter("id");
            res.put("id", id);
            externalEvalMapperService.delete(res);
            res.put("success", "操作成功!");
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }
}
